package com.robot.merchant.controller;

import com.alibaba.fastjson.JSON;
import com.robot.merchant.pojo.Industry;
import com.robot.merchant.service.IndustryService;
import com.robot.merchant.service.StoreService;
import com.robot.merchant.service.TagService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * industry控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/industry")
public class IndustryController {
    @Autowired
    private IndustryService industryService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private TagService tagService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询全部并根据权重排序
     *
     * @return
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public Result findAll() {
        List<Industry> industryList=null;
        //这里可以做一个加强，判断一下是否生产环境，如果不是则不缓存
        if(redisTemplate.hasKey("industries")){
            logger.info("from redis");
            industryList=redisTemplate.opsForList().range("industries",0,-1);
        }else{
            logger.info("from db");
            industryList = industryService.findAll();
            for(Industry industry:industryList){
                redisTemplate.opsForList().rightPush("industries",industry);
            }
        }
        return new Result(StatusCode.SUCCESS_QUERY, industryList);
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Industry> pageList = industryService.findSearch(searchMap, page, size);
        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<Industry>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(StatusCode.SUCCESS_QUERY, industryService.findSearch(searchMap));
    }

    /**
     * 查询全部并根据权重排序
     *
     * @return
     */
    @RequestMapping(value = "/getIndustryList", method = RequestMethod.GET)
    public Result getIndustryList() {
        List<Industry> industries = industryService.findAll();
        return new Result(StatusCode.SUCCESS_QUERY, industries);
    }
}
